class InitialDatabase < ActiveRecord::Migration
  def self.up
    create_table "customer_records" do |t|
      t.timestamps
      t.column :user_id, :integer
      t.column :fname, :string
      t.column :mname, :string
      t.column :lname, :string
      t.column :street, :string
      t.column :state, :string
      t.column :zip, :integer
      t.column :mobile, :integer
      t.column :work, :integer
      t.column :email, :string
      t.column :notes, :string
      t.column :contact_preference, :string #prefers to be contacted by email, phone, etc.
      t.column :customer_state_id, :integer
    end
    
    create_table "repair_records" do |t|
      t.timestamps
      t.column :user_id, :integer
      ### the items below will never change once a repair is created
      t.column :customer_record_id, :integer
      t.column :cmodel_id, :integer # the cmodel will contain the manufacturer information
      t.column :computer_serial, :string
      t.column :checkin_condition, :string
      t.column :ram_size, :string
      t.column :hdd_size, :string
      ### the items below will be updated every time a new triage_note is created
      t.column :issue_id, :integer
      t.column :issue_name, :string # auto populated from the template_issue table (for record's sake)
      t.column :parts_ordered, :string #an autopopulated list of names and cost estimates of parts associated with repair
      t.column :note_title, :string
      t.column :note, :string # this will always be the latest note
      t.column :computer_in_house, :boolean
      t.column :repair_state_id, :integer
      t.column :time_estimate, :datetime
      ### the pos_number will only be updated when customer pays for repair. A repair with a POS number will be forced to a "closed" status
      t.column :pos_number, :string #a repair that is "closed" with no POS number will remain on the open repairs list. if repair is cancelled, POS # should be "cancelled"
    end
    
    create_table "triage_notes" do |t|
      t.timestamps
      t.column :user_id, :integer
      t.column :repair_record_id, :integer
      t.column :customer_call_flag, :boolean # mark TRUE if this note represents a customer call
      ### the items below will update their corresponding columns in the appropriate repair_record when saved
      t.column :issue_id, :integer
      t.column :issue_name, :string # auto populated from the template_issue table (for record's sake)
      t.column :parts_ordered, :string #an autopopulated list of names and cost estimates of parts associated with repair
      t.column :note_title, :string
      t.column :note, :string
      t.column :computer_in_house, :boolean
      t.column :repair_state_id, :integer # open, awaiting triage, awaiting parts, in repair, waiting for customer callback, repair complete, closed (cancelled)
      t.column :time_estimate, :datetime
    end
    # script/generate scaffold TriageNote created_by_id:integer created_at:datetime repair_record_id:integer customer_call_flag:boolean issue_id:integer issue_name:string parts_ordered:string note_title:string note:string computer_in_house:boolean repair_state_id:integer time_estimate:integer
    
    create_table "part_records" do |t|
      t.timestamps
      t.column :user_id, :integer
      ### items below are generated when the part is first added to a repair record
      t.column :repair_record_id, :integer
      t.column :template_part_id, :integer #this way i can pull across generic information from the template to support the diagnosis and estimate
      t.column :name, :string
      t.column :supplier, :string
      t.column :notes, :string
      t.column :cost_estimate, :integer
      t.column :delivery_estimate, :datetime
      ### items below are added later
      t.column :tracking_number, :string
      t.column :cost_actual, :integer
      t.column :delivery_actual, :datetime
      t.column :part_state_id, :integer
      ### items below are added if repair is completed successfully
      t.column :issue_id, :integer
    end
    
    create_table "customer_states" do |t| # the various states a customer record can be in
      t.timestamps
      t.column :user_id, :integer
      t.column :name, :string
    end
    create_table "repair_states" do |t| #the varius states a repair can be in
      t.timestamps
      t.column :user_id, :integer
      t.column :name, :string # open, awaiting triage, awaiting parts, in repair, waiting for customer callback, repair complete, closed (cancelled)
    end
    create_table "part_states" do |t| #the various states a part can be in
      t.timestamps
      t.column :user_id, :integer
      t.column :name, :string #awaiting order, ordered, in-transit, stocked, allocated
    end
    
    
    
    
    
    ############ everything below this line merely exists to help support the filling in of information when adding, modifying, or completing a repair
    
    ############ templates
    create_table "template_parts" do |t|
      t.timestamps
      t.column :user_id, :integer
      t.column :name, :string
      t.column :supplier, :string
      t.column :notes, :string
      t.column :delivery_time_estimate, :datetime # numerical value represents days
      t.column :cost_estimate, :integer
      t.column :delivery_estimate, :integer
      t.column :cmodel_id, :integer
    end
    create_table "manufacturers" do |t|
      t.timestamps
      t.column :user_id, :integer
      t.column :name, :string
      t.column :contact_phone, :integer
      t.column :notes, :string
    end
    create_table "cmodels" do |t|
      t.timestamps
      t.column :user_id, :integer
      t.column :manufacturer_id, :integer
      t.column :name, :string
      t.column :support_link, :string
      t.column :manual_link, :string
      t.column :notes, :string
    end
    ############ repair categories (for sorting and diagnosing future repairs)
    create_table "issues_containers" do |t| #for instance, "video issue"
      t.timestamps
      t.column :user_id, :integer
      t.column :name, :string
      t.column :description, :string
    end
    create_table "issues" do |t| #for instance "garbled video" or "no backlight"
      t.timestamps
      t.column :user_id, :integer
      t.column :cmodel_id, :integer #computer cmodel to which this issue belongs
      t.column :issue_container_id, :integer #for instance: belongs in the container of "video issue" which is ID 4
      t.column :description, :string
      t.column :notes, :string
    end
  end




  def self.down
    drop_table "customer_records" 
    drop_table "repair_records"
    drop_table "triage_notes" 
    drop_table "part_records" 
    drop_table "customer_states"
    drop_table "repair_states" 
    drop_table "part_states" 
    drop_table "template_parts"
    drop_table "manufacturers" 
    drop_table "cmodels" 
    drop_table "issues_containers" 
    drop_table "issues" 
  end
end

